package com.javafactroy.kunlong.common.utils;


import com.javafactroy.kunlong.common.dto.RedisKeyDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@ConditionalOnProperty(prefix = "spring.redis", name = "enabled", havingValue = "true")

public class RedisApiUtil extends RedisUtil {

    @Autowired
    StringRedisTemplate redisTemplate;

    String fullKey(String key) {
        return new RedisKeyDto().setKey(key).fullKey();
    }

    public void put(String key, String value) {
        super.put(fullKey(key), value);

    }

    public String get(String key) {
        return super.get(fullKey(key));

    }


    public void set(String key, String val) {
        super.set(fullKey(key), val);
    }

    /**
     * @param key
     * @param val
     * @param time s
     */
    public void set(String key, String val, long time) {
        super.set(fullKey(key), val, time);
    }

    public Boolean setnx(String key, String val) {
        return super.setnx(fullKey(key), val);
    }

    public Map<Object, Object> getHash(String key) {
        return super.getHash(fullKey(key));
    }

    public String getValue(String key) {
        return super.getValue(fullKey(key));
    }

    /**
     * 设置Hash值
     *
     * @param key
     * @param propName
     * @param propValue
     * @param
     */
    public void putHash(String key, String propName, Object propValue) {
        super.putHash(fullKey(key), propName, propValue);

    }

    public void putHashs(String key, Map<Object, Object> values) {
        super.putHashs(fullKey(key), values);
    }

    public BoundHashOperations<String, Object, Object> initHash(String key, long timeout) {

        return super.initHash(fullKey(key), timeout);
    }

    public Object getHash(String key, String propName) {

        return super.getHash(fullKey(key), propName);
    }


    public void delete(String key) {
        super.delete(fullKey(key));
    }

    /**
     * 设置超时
     *
     * @param key
     * @param timeoutSeconds
     */
    public void expireValue(String key, Long timeoutSeconds) {

        super.expireValue(fullKey(key), timeoutSeconds);
    }

    /**
     * 获取超时时间
     *
     * @param key
     * @return 超时时间（秒）
     */
    public Long getExpireValue(String key) {
        return super.getExpireValue(fullKey(key));
    }


    public boolean hasKey(String key) {
        return super.hasKey(fullKey(key));
    }

    public Boolean expire(String key, long timeout) {

        return super.expire(fullKey(key), timeout);
    }

    public Long getExpire(String key) {
        return super.getExpire(fullKey(key));
    }

    public long incr(String key, long delta) {
        return super.incr(fullKey(key), delta);
    }


}
